package xcmg.device.web.delivery;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.delivery.DeliveryReceiptLogisticsService;
import xcmg.device.service.delivery.DeliveryReceiptService;
import xcmg.device.service.logistics.SendBillService;
import xcmg.device.service.vo.delivery.DeliveryReceiptLogisticsVO;
import xcmg.device.service.vo.logistics.SendBillVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/deliveryReceiptLogistics")
public class DeliveryReceiptLogisticsController extends BasicController{

    @Autowired
    private DeliveryReceiptLogisticsService deliveryReceiptLogisticsService;

    @Autowired
    private DeliveryReceiptService deliveryReceiptService;

    @Autowired
    private SendBillService sendBillService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody DeliveryReceiptLogisticsVO deliveryReceiptLogisticsVO){
        if(deliveryReceiptLogisticsVO == null){
            return actionResult(ErrorCode.IllegalArument);
        }
        deliveryReceiptLogisticsVO.setCreateId(getUserId());
        return actionResult(deliveryReceiptLogisticsService.add(deliveryReceiptLogisticsVO));
    }
    @RequestMapping(value = "remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids){
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=deliveryReceiptLogisticsService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<SendBillVO> condition)throws Exception{
        SendBillVO sendBillVO =condition.getFilters();
        if(sendBillVO == null){
            sendBillVO = new SendBillVO();
        }
        //承运公司编号
        sendBillVO.setCarrierOrgId(getOrgId());
        condition.setFilters(sendBillVO);
        PageCommonVO pageCommonVO= sendBillService.list4Logistics(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/list4Dealer",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list4Dealer(@RequestBody SearchCommonVO<SendBillVO> condition)throws Exception{
        SendBillVO sendBillVO =condition.getFilters();
        if(sendBillVO == null){
            sendBillVO = new SendBillVO();
        }
        //销售公司编号
        sendBillVO.setSoldOrgId(getOrgId());
        condition.setFilters(sendBillVO);
        PageCommonVO pageCommonVO= sendBillService.list4Logistics(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(ErrorCode.Success,deliveryReceiptLogisticsService.item(id));
    }

    @RequestMapping(value = "/item4Logistics",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item4Logistics(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(ErrorCode.Success,deliveryReceiptLogisticsService.item4Logistics(id));
    }



}
